﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using YArchitech.Revit.MEP.Opening;

namespace HYRMEOpening
{
	internal class ShowDlgApp : IExternalApplication
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result OnShutdown(UIControlledApplication application)
		{
			if (this.mDod != null && !this.mDod.IsDisposed)
			{
				this.mDod.Dispose();
				this.mDod = null;
				this.mDco = null;
				this.mDlgCreate = null;
				application.Idling -= this.IdlingHandler;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result OnStartup(UIControlledApplication application)
		{
			this.mDco = null;
			this.mDlgCreate = null;
			this.mDod = null;
			ShowDlgApp.ThisApp = this;
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public void ShowDlgOpeningDetect(List<DbOpeningBase> ol, UIApplication app, bool server, bool isLinkModel, string fileName)
		{
			if (this.mDod == null || this.mDod.IsDisposed)
			{
				if (ShowDlgApp._hWndRevit == null)
				{
					ShowDlgApp._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				this.mDod = DlgOpeningDetect.GetInstance(ol, app, server, isLinkModel, fileName);
				this.mDod.Show(ShowDlgApp._hWndRevit);
				int wMsg = 7;
				ShowDlgApp.SendMessage(ShowDlgApp._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				app.Idling += this.IdlingHandler;
				this.mDco = null;
				this.mDlgCreate = null;
			}
		}

		public void CloseDlgOpeningDetect()
		{
			if (this.mDod != null || !this.mDod.IsDisposed)
			{
				this.mDod.Close();
			}
		}

		public void ShowDlgCheckOpening(UIApplication app, bool server, string fileName)
		{
			if (this.mDod == null || this.mDod.IsDisposed)
			{
				if (ShowDlgApp._hWndRevit == null)
				{
					ShowDlgApp._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				this.mDco = DlgCheckOpening.GetInstance(app.ActiveUIDocument.Application.ActiveUIDocument, server, fileName);
				this.mDco.Show(ShowDlgApp._hWndRevit);
				int wMsg = 7;
				ShowDlgApp.SendMessage(ShowDlgApp._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				app.Idling += this.IdlingHandler;
				this.mDod = null;
				this.mDlgCreate = null;
			}
		}

		public void CloseDlgCheckOpening()
		{
			if (this.mDco != null || !this.mDco.IsDisposed)
			{
				this.mDco.Close();
			}
		}

		public void ShowDlgCreateOpening(UIApplication app, bool server, string fileName)
		{
			if (this.mDod == null || this.mDod.IsDisposed)
			{
				if (ShowDlgApp._hWndRevit == null)
				{
					ShowDlgApp._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				this.mDlgCreate = DlgCreateOpening.GetInstance(app, server, fileName);
				this.mDlgCreate.Show(ShowDlgApp._hWndRevit);
				int wMsg = 7;
				ShowDlgApp.SendMessage(ShowDlgApp._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				app.Idling += this.IdlingHandler;
				this.mDod = null;
				this.mDco = null;
			}
		}

		public void CloseDlgCreateOpening()
		{
			if (this.mDlgCreate != null || !this.mDlgCreate.IsDisposed)
			{
				this.mDlgCreate.Close();
			}
		}

		public void IdlingHandler(object sender, IdlingEventArgs args)
		{
			UIApplication uiapplication = sender as UIApplication;
			RequestId requestId = RequestId.None;
			BindingList<DbOpeningBase> opiningList = new BindingList<DbOpeningBase>();
			if (this.mDod != null)
			{
				requestId = this.mDod.Request.Take();
				opiningList = this.mDod.OpiningList;
			}
			else if (this.mDco != null)
			{
				requestId = this.mDco.Request.Take();
				opiningList = this.mDco.OpiningList;
			}
			else if (this.mDlgCreate != null)
			{
				requestId = this.mDlgCreate.Request.Take();
			}
			if (requestId == RequestId.ClearTag)
			{
				RequestHandler.ClearTag(uiapplication);
			}
			else
			{
				if (requestId == RequestId.AddTag)
				{
					try
					{
						RequestHandler.ClearTag(uiapplication);
						RequestHandler.AddTag(uiapplication, opiningList);
						goto IL_15B;
					}
					catch (Exception)
					{
						goto IL_15B;
					}
				}
				if (requestId == RequestId.ChangeOpeningStyle)
				{
					RequestHandler.ChangeTag(uiapplication, this.mDod.OpeningToChange);
				}
				else if (requestId == RequestId.NewTag)
				{
					this.mDod.NewTag.AddOpeningTag(uiapplication, TagStyle.PositionAndSize);
				}
				else if (requestId == RequestId.DelTag)
				{
					this.mDod.DelOpening.RemoveOpeningTag(uiapplication.ActiveUIDocument.Document);
				}
				else if (requestId == RequestId.Regenerate)
				{
					if (this.mDod != null)
					{
						RequestHandler.Regenerate(this.mDod.UiDoc);
					}
					else if (this.mDco == null && this.mDlgCreate != null)
					{
						RequestHandler.Regenerate(this.mDlgCreate.UiDoc);
					}
				}
				else if (requestId == RequestId.DelOpening)
				{
					RequestHandler.DelOpening(this.mDco.mUiDoc.Document, this.mDco.FamilyInstanceList);
				}
			}
			IL_15B:
			new FilteredElementCollector(uiapplication.ActiveUIDocument.Document).OfClass(typeof(Group)).ToElements();
		}

		internal static ShowDlgApp ThisApp;

		public DlgOpeningDetect mDod;

		public DlgCheckOpening mDco;

		public DlgCreateOpening mDlgCreate;

		public static WindowHandle _hWndRevit;
	}
}
